const BASE_URL = 'http://127.0.0.1:5000/api_1_0'

// 公共请求函数
function request(options) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + options.url,
            method: options.method || 'GET',
            data: options.data,
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                ...(options.header || {})
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                console.error(`${options.url} 请求失败:`, err)
                reject(err)
            }
        })
    })
}

// 需要token的请求
function requestWithToken(options) {
    const authorization = uni.getStorageSync('token')
    console.log('获取到的原始token:', authorization)
    
    if (!authorization) {
        console.warn('token不存在，请重新登录')
        return Promise.reject('未登录')
    }
    
    const token = authorization.replace('Bearer ', '')
    console.log('处理后的token:', token)
    
    return request({
        ...options,
        header: {
            'Token': token,
            ...(options.header || {})
        }
    })
}

// 导出的接口函数
export function register(data) {
    return request({
        url: '/user/register',
        method: 'POST',
        data
    })
}

export function login(data) {
    return request({
        url: '/user/login',
        method: 'POST',
        data
    })
}

export function modifyPassword(data) {
    return requestWithToken({
        url: '/user/modifyPassword',
        method: 'PUT',
        data
    })
}

export function me() {
    return requestWithToken({
        url: '/user/me',
		method: 'GET'
    })
}

export function dictionary() {
    return request({
        url: '/dictionary'
    })
}

export function plan(data) {
    return requestWithToken({
        url: '/plan/save',
        method: 'POST',
        data
    })
}

export function next_word(planId) {
    return requestWithToken({
        url: `/word/next-word/${planId}`,
        method: 'GET'
    })
}

export function records() {
    return requestWithToken({
        url: '/userwordtimes/records',
        method: 'GET'
    })
}

export function get_approved_articles() {
    return requestWithToken({
        url: '/article/get_approved_articles'
    })
}

export function add_article(data) {
    return requestWithToken({
        url: '/article/add_article',
        method: 'POST',
        data
    })
}

export function user_wordbooks(data) {
    return requestWithToken({
        url: '/wordbook/user_wordbooks',
        method: 'POST',
        data
    })
}


export function create(data) {
    return requestWithToken({
        url: '/wordbook/create',
		 method: 'POST',
        data
    })
}

export function is_get(wordId) {
    return requestWithToken({
        url: `/word/is_get/${wordId}`,
        method: 'POST'
    })
}


export function add_word_to_wordbook(data) {
    return requestWithToken({
        url: '/wordbook/add_word_to_wordbook',
		 method: 'POST',
        data
    })
}

export function test(dictionary_id) {
    return requestWithToken({
        url: `/dictionary/${dictionary_id}/generate-test`,
        method: 'GET'
    })
}





export function top_word(wordbook_id) {
    return requestWithToken({
        url: `/wordbook/${wordbook_id}/words`,
        method: 'GET'
    })
}
export function bottom_word(wordbook_id) {
    return requestWithToken({
        url: `/wordbook/${wordbook_id}/words`,
        method: 'GET'
    })
}